<template>
	<view class="detail">
		<view class="detail-title">
			{{formData.title}}
		</view>
		<view class="detail-header">
			<view class="detail-header__logo">
				<image :src="formData.authorAvatar" mode="aspectFill"></image>
			</view>
			<view class="detail-header__content">
				<view class="detail-header__content-title">
					{{formData.authorNickName}}
				</view>
				<view class="detail-header__content-info">
					<text>{{formData.createTime}}</text>
					<text>{{formData.browseCount}} 浏览</text>
					<text>{{formData.thumbsUpCount}} 赞</text>
				</view>
			</view>
			<button v-if="this.formData.authorName!=this.currentUserName" class="detail-header__button" type="default" @click="follow(formData.authorName)">{{formData.isAuthorLike == 1?'取消关注':'关注'}}</button>
		</view>
		<view class="detail-content">
			<view class="detail-html">
				<u-parse :content="formData.content" :noData="noData"></u-parse>
			</view>
			<view class="detail-comment">
				<view class="comment-title">最新评论</view>
				<view class="comment-content" v-for="item in commentsList" :key="item.comment_id">
					<comments-box :comments="item" @reply="reply"></comments-box>
				</view>
			</view>
		</view>
		<view class="detail-bottom">
			<view class="detail-bottom__input" @click="openComment">
				<text>谈谈你的看法</text>
				<uni-icons type="compose" size="16" color="#037aff"></uni-icons>
			</view>
			<view class="detail-bottom__icons">
				<view class="detail-bottom__icons-box"> <!-- @click="open" -->
					<uni-icons type="chat" size="22" color="#037aff"></uni-icons>
				</view>
				<view class="detail-bottom__icons-box"> <!-- @click="likeTap(formData.articleId)" -->
					<uni-icons :type="formData.isLike?'heart-filled':'heart'" size="22" color="#037aff"></uni-icons>
				</view>
				<view class="detail-bottom__icons-box"> <!-- @click="thumbsup(formData.articleId)" -->
					<uni-icons :type="formData.isThumbsUp?'hand-up-filled':'hand-up' " size="25" color="#037aff"></uni-icons>
				</view>
			</view>
		</view>
		<release ref="popup" @submit="submit"></release>
	</view>
</template>

<script>
	import uParse from '@/components/gaoyia-parse/parse.vue'
	import config from '@/config'
	import {
		getArticleDetail
	} from '@/api/info/article.js'
	import {
		follow
	} from '@/api/info/follow.js'
	import {
		addComment,
		getArticleCommentList
	} from '@/api/info/comment.js'
	export default {
		components:{
			uParse
		},
		data() {
			return {
				currentUserName: this.$store.state.user.name,
				formData: {},
				noData:'<p style="text-align:center;color:#666">详情加载中...</p>',
				commentsList:[],
				replyFormData:{},
				showPopup:false
			}
		},
		onLoad(event) {
			this.formData = JSON.parse(decodeURIComponent(event.payload))
			console.log(this.formData)
			this.getDetail()
			this.getComments()
		},
		methods: {
			/**
			 * 打开评论列表
			 */
			open(){
				uni.navigateTo({
					url:'../detail-comments/detail-comments?id='+this.formData._id
				})
			},
			// 点赞
			thumbsup(article_id){
				this.setUpdateThumbs(article_id)
			},
			// 收藏
			likeTap(article_id){
				console.log('收藏文章');
				this.setUpadteLike(article_id)
			},
			// 关注
			follow(authorName){
				console.log('关注');
				this.setUpdateAuhtor(authorName)
			},
			// 打开评论发布窗口
			openComment(){
				this.$refs.popup.open()
			},
			// 关闭弹窗
			close(){
				this.$refs.popup.close()
			},
			// 发布
			submit(comment){
				this.setUpdateComment({comment,...this.replyFormData})
			},
			reply(e){
				this.replyFormData = {
					"comment_id":e.comments.comment_id,
					"is_reply": e.is_reply
				}
				if(e.comments.reply_id){
					this.replyFormData.reply_id = e.comments.reply_id
				}
				console.log(this.replyFormData);
				this.openComment()
			},
			setUpdateComment(comment){
				const commentData ={
					objectId:this.formData.articleId,
					objectType:'com.bmz.article',
					...comment
				}
				console.log(commentData);
				uni.showLoading()
				addComment(commentData).then((res)=>{
					console.log(res);
					uni.hideLoading()
					uni.showToast({
						title:'评论发布成功'
					})
					this.getComments()
					this.close()
					this.replyFormData = {}
				})
			},
			// 获取详情信息
			getDetail() {
				getArticleDetail(this.formData.articleId).then((res) => {
					const {data} = res
					this.formData = data
					this.formData.authorName = data.authorInfo.userName
					this.formData.authorNickName = data.authorInfo.nickName
					this.formData.authorAvatar = config.baseUrl + data.authorInfo.avatar
					
					console.log(this.formData)
				});
			},
			// 请求评论内容
			getComments(){
				getArticleCommentList(this.formData.articleId).then(res=>{
					console.log(res);
					const {rows} = res
					this.commentsList = rows
				})
			},
			// 关注作者
			setUpdateAuhtor(authorName){
				uni.showLoading()
				follow({
					authorName,
					isAuthorLike: this.formData.isAuthorLike
				}).then(res=>{
					uni.hideLoading()
					this.formData.isAuthorLike = (this.formData.isAuthorLike == 1) ? 2 : 1;
					uni.$emit('update_author')
					uni.showToast({
						title:this.formData.isAuthorLike == 1?'关注作者成功':'取消关注作者',
						icon:'none'
					})
				})
			},
			// 收藏文章 
			setUpadteLike(article_id){
				uni.showLoading()
				this.$api.update_like({
					article_id
				}).then(res=>{
					uni.hideLoading()
					this.formData.isLike = !this.formData.isLike
					uni.$emit('update_article','follow')
					uni.showToast({
						title:this.formData.isLike ?'收藏成功':'取消收藏',
						icon:'none'
					})
					console.log('收藏成功');
				})
			},
			setUpdateThumbs(article_id){
				uni.showLoading()
				this.$api.update_thumbsup({
					article_id
				}).then(res=>{
					uni.hideLoading()
					this.formData.is_thumbs_up = true
					this.formData.thumbs_up_count++
					uni.showToast({
						title:res.msg
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.detail {
		padding: 15px 0;
		padding-bottom: 54px;
		background-color: #fff;
	}

	.detail-title {
		padding: 0 15px;
		font-size: 18px;
		font-weight: bold;
		color: #333;
	}

	.detail-header {
		display: flex;
		align-items: center;
		margin-top: 10px;
		padding: 0 15px;

		.detail-header__logo {
			flex-shrink: 0;
			width: 40px;
			height: 40px;
			border-radius: 50%;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.detail-header__content {
			width: 100%;
			padding-left: 10px;
			display: flex;
			flex-direction: column;
			justify-content: space-between;
			font-size: 12px;

			.detail-header__content-title {
				font-size: 14px;
				color: #333;
			}

			.detail-header__content-info {
				color: #999;

				text {
					margin-right: 10px;
				}
			}
		}
		.detail-header__button {
			padding: 0 15px;
			flex-shrink: 0;
			height: 30px;
			line-height: 30px;
			border-radius: 5px;
			font-size: 12px;
			color: #fff;
			background-color: $mk-base-color;
		}
	}

	.detail-content {
		margin-top: 20px;
		min-height: 500px;
		.detail-html {
			padding:0 15px;
		}
		.detail-comment {
			margin-top: 30px;
			.comment-title {
				padding: 10px 15px;
				font-size: 14px;
				color: #666;
				border-bottom: 1px #f5f5ff solid;
			}
			.comment-content {
				padding: 0 15px;
				border-top: 1px #eee solid;
			}
		}
	}

	.detail-bottom {
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		align-items: center;
		width: 100%;
		height: 44px;
		border-top: 1px #f5f5f5 solid;
		background-color: #fff;
		box-sizing: border-box;

		.detail-bottom__input {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-left: 10px;
			padding: 0 10px;
			width: 100%;
			height: 30px;
			border: 1px #ddd solid;
			border-radius: 5px;

			text {
				font-size: 14px;
				color: #999;
			}
		}

		.detail-bottom__icons {
			display: flex;
			flex-shrink: 0;
			padding: 0 10px;

			.detail-bottom__icons-box {
				position: relative;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 44px;
			}
		}
	}
	

</style>
